package gulimall.web;

import gulimall.entity.Category;
import gulimall.service.ICategoryService;
import gulimall.vo.web.Catelog2Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author HLF
 * @date 2022/6/16 14:27
 */
@Controller
public class IndexController {

    @Resource
    private ICategoryService categoryService;

    /**
     * 跳转到首页
     */
    @GetMapping({"/", "/index.html"})
    public String index(Model model) {
        List<Category> list = categoryService.listLevel1();

        model.addAttribute("categorys", list);

        return "index";
    }

    /**
     * 查询二级和三级分类信息
     * 查询到一级分类，根据一级分类查询出二级分类并设置对应Vo对象，以此类推
     *
     * @return 返回分类信息的json字符串，也可以直接返回Map集合，SpringMVC会自动解析成JSON字符串
     */
    @GetMapping("/index/getCatalogJson")
    @ResponseBody
    public Object getCatalogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCategoryJson();
        return map;
    }
}
